package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.ex.ServiceCode;
import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.BrandUpdateNewDTO;
import cn.tedu.csmall.product.pojo.entity.Brand;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

//@Controller
//@ResponseBody//服务器默认返回一个页面 加了该注解默认返回正文
@Api(tags = "02.品牌管理模块")
@RestController
@Slf4j
@Validated
@RequestMapping("/brands")
public class BrandController {
    @Autowired
    private IBrandService brandService;
    @ApiOperation("添加品牌")
    @PostMapping("/add-new")
    @ApiOperationSupport(order = 100)
    //http://localhost:9080/add-new?name=华为&logo=www.lll&pinyin=huawei&sort=97&description=我最好&enable=1&keywords=hahah
    public JsonResult<Void> addNew(@Valid BrandAddNewDTO brandAddNewDTO){
        System.out.println("即将处理【添加品牌】的请求……");
        System.out.println("brandAddNewDTO = " + brandAddNewDTO);

         brandService.addNew(brandAddNewDTO);
            return JsonResult.ok();


    }

    // http://localhost:9080/delete
    @ApiOperation("删除品牌")
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id",value = "品牌id",required = true,dataType =
    "long")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("即将处理【删除品牌】的请求，id={}", id);

            brandService.deleteById(id);
            return JsonResult.ok();



    }
    // http://localhost:9080/brands/1/enable
    @ApiOperation("启用品牌")
    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id",value = "品牌id",required = true,dataType =
            "long")
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        log.debug("即将处理【启用品牌】的请求，id={}", id);
            brandService.setEnable(id);
            return JsonResult.ok();


    }
    // http://localhost:9080/brands/3/disable
    @ApiOperation("禁用品牌")
    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id",value = "品牌id",required = true,dataType =
            "long")
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        log.debug("即将处理【禁用品牌】的请求，id={}", id);
            brandService.setDisable(id);
            return JsonResult.ok();



    }
    // http://localhost:9080/brands/update
    @ApiOperation("修改品牌")
    @ApiOperationSupport(order = 400)
    @PostMapping("/update")
    public JsonResult<Void> update(@Valid BrandUpdateNewDTO brandUpdateNewDTO){
        log.debug("即将处理【修改品牌】的请求，brandUpdateNewDTO={}", brandUpdateNewDTO);
        brandService.updateById(brandUpdateNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/brands/hello/delete
    @ApiOperation("测试根据名称删除品牌")
    @ApiOperationSupport(order = 900)
    @PostMapping("/{name:[a-zA-Z]+}/delete")
    public String delete(@PathVariable String name) {
        log.debug("接收到【根据名称删除品牌（测试）】的请求，品牌名={}", name);
        throw new RuntimeException("此接口仅用于测试，并未实现任何功能！");
    }

    // http://localhost:9080/brands/test/delete
    //精准匹配优先级高
    @ApiOperation("测试删除品牌")
    @ApiOperationSupport(order = 901)
    @GetMapping("/test/delete")
    @Deprecated
    @ApiImplicitParam(name = "enable",dataType = "int",paramType = "query")
    public String delete(@Range(max=1,message = "测试失败,enable的值必须是0或1") Integer enable) {
        log.debug("接收到【删除品牌（测试）】的请求");
        throw new RuntimeException("此接口仅用于测试，并未实现任何功能！");
    }


}
